package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.alarm.pojo.AlarmParameterSettings;
import cn.darkhorse.alarm.service.AlarmParameterSettingsService;
import cn.darkhorse.device.pojo.IotInstructParams;
import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.service.IotInstructParamsService;
import cn.darkhorse.device.service.MesDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangtianqi
 * @since 2024-10-22
 */
@RestController
@RequestMapping("/device/iotInstructParams")
public class IotInstructParamsController extends BaseController {

    @Autowired
    private IotInstructParamsService iotInstructParamsService;
    @Autowired
    private MesDeviceService mesDeviceService;
    @Autowired
    private AlarmParameterSettingsService alarmParameterSettingsService;


    @RequestMapping("/list")
    public TableDataInfo listAll(IotInstructParams params){
        startPage();

        LambdaQueryWrapper<IotInstructParams> queryWrapper = new LambdaQueryWrapper<>();
        if (params.getDeviceId() != null){
//            if (params.getIsActive()!=null && params.getIsActive() == 1){
//                List<MesDevice> devices = mesDeviceService.lambdaQuery().eq(MesDevice::getParentId, params.getDeviceId()).list();
//                if (devices != null && !devices.isEmpty()){
//                    List<Long> ids = devices.stream().map(MesDevice::getId).collect(Collectors.toList());
//                    ids.add(params.getDeviceId());
//
//                    queryWrapper.in(IotInstructParams::getDeviceId, ids);
//                }
//            }else{
                queryWrapper.eq(IotInstructParams::getDeviceId, params.getDeviceId());
//            }
        }
        if (params.getGatewayDeviceId() != null){
            queryWrapper.eq(IotInstructParams::getGatewayDeviceId, params.getGatewayDeviceId());
        }
        if (params.getType() != null){
            queryWrapper.eq(IotInstructParams::getType, params.getType());
        }
        if (params.getIsActive() != null){
            queryWrapper.eq(IotInstructParams::getIsActive, params.getIsActive());
        }


        List<IotInstructParams> list = iotInstructParamsService.list(queryWrapper);
        return getDataTable(list);
    }

    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult save(@Validated @RequestBody IotInstructParams instructParams) {
        boolean b = iotInstructParamsService.saveOrUpdate(instructParams);
        return AjaxResult.success(b);
    }

    @RequestMapping("/delete")
    public AjaxResult delete(Long id) {
        boolean b = iotInstructParamsService.removeById(id);
        if (b){
            alarmParameterSettingsService.lambdaUpdate().eq(AlarmParameterSettings::getInstructId, id)
                    .set(AlarmParameterSettings::getSendToGateway,0)
                    .set(AlarmParameterSettings::getInstructId,null).update();
        }
        return AjaxResult.success(b);
    }

    @RequestMapping("/view")
    public AjaxResult view(Long id) {
        IotInstructParams byId = iotInstructParamsService.getById(id);
        return AjaxResult.success(byId);
    }




}

